//
//  ViewController.m
//  28-UIViewAnimation
//
//  Created by dllo on 16/8/22.
//  Copyright © 2016年 DM.C. All rights reserved.
//

#import "ViewController.h"
#import "UIView+Frame.h"
@interface ViewController ()
@property (nonatomic, strong) UIView *animationView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.animationView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    _animationView.backgroundColor = [UIColor redColor];
    [self.view addSubview:_animationView];
    
    // 1.UIView Animation
    //  1>开始动画设置
    // 参数1:动画标识 (animationID)
    // 参数2:上下文 (context)
#if 0
    [UIView beginAnimations:nil context:NULL];
    /** 参数: UIViewAnimationCurve枚举类型
     typedef NS_ENUM(NSInteger, UIViewAnimationCurve) {
     UIViewAnimationCurveEaseInOut,         // 开始和结束时满
     UIViewAnimationCurveEaseIn,            // 开始时慢
     UIViewAnimationCurveEaseOut,           // 结束时慢
     UIViewAnimationCurveLinear,            // 匀速
     };
     */
    //  2>.设置动画的曲线(运动方式, 如上面的枚举类型)
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    //  3>.设置动画持续时间
    [UIView setAnimationDuration:10];
    //  4>.动画的延时时间(延时多少秒才开始动画)
//    [UIView setAnimationDelay:5];
    //  5>.动画重复次数
//    [UIView setAnimationRepeatCount:2.5];
    //  6>.设置往返动画, 一个来回算一次(默认为NO)
    [UIView setAnimationRepeatAutoreverses:YES];
    //  7>.忽略动画过程
    [UIView setAnimationsEnabled:NO];
    //  8>.动画的改变
    _animationView.dm_width = self.view.dm_width / 2.0;
    _animationView.dm_height = self.view.dm_height / 2.0;
    _animationView.dm_centerX = self.view.dm_centerX;
    _animationView.dm_centerY = self.view.dm_centerY;
    //  9>.动画的代理, 不签订代理, 代理方法不执行(默认为不签订)
    //      将要开始
    //      已经结束
    [UIView setAnimationDelegate:self];
    [UIView setAnimationWillStartSelector:@selector(animationWillStart:context:)];
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
    //  10>.结束动画设置, 执行该动画
    [UIView commitAnimations];
#endif
    // 2.UIViewAnimationBlock
    //  1> UIViewBlockAnimation
    //  参数1: 持续时间
    //  参数2: block, 需要执行的动画效果
//    [UIView animateWithDuration:5 animations:^{
//        self.animationView.dm_width = self.view.dm_width;
//    }];
    //  2>
    //  参数3:动画完成后执行的操作
//    [UIView animateWithDuration:5 animations:^{
//        _animationView.dm_x = 300;
//    } completion:^(BOOL finished) {
//        NSLog(@"动画完成后, 需要执行的操作");
//    }];
    //  3>
    //  参数2: 延时
    //  参数3: 选项枚举, 按位枚举, 能同时多选
//    [UIView animateWithDuration:5 delay:2 options:UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse  animations:^{
//        _animationView.dm_width = self.view.dm_width;
//    } completion:^(BOOL finished) {
//        NSLog(@"动画完成后, 执行的操作");
//    }];
    
    [UIView animateWithDuration:5 delay:1 usingSpringWithDamping:1 initialSpringVelocity:0.1 options:UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse animations:^{
        _animationView.dm_width = self.view.dm_width;
        _animationView.backgroundColor = [UIColor cyanColor];
        _animationView.layer.cornerRadius = self.view.dm_width / 2;
    } completion:^(BOOL finished) {
        NSLog(@"动画完成后, 执行的操作");
    }];
 
    
    
}
#pragma mark - 两个协议方法
- (void)animationWillStart:(NSString *)animationID context:(void *)context {
    NSLog(@"动画将要开始");
}
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
    NSLog(@"动画已经结束");
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
    
}


@end
